package com.walker.elk.netty.protobuf;


import com.walker.elk.netty.protobuf.client.ProtoBufClientChannelInitializer;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class WalkerProtoBufClient {
	
	private String address;
	
	private int port;

	public WalkerProtoBufClient() {
		super();
	}

	public WalkerProtoBufClient(String address, int port) {
		super();
		this.address = address;
		this.port = port;
	}

	public void start() {
		//客户端只需要一个
		EventLoopGroup eventLoopGroup = new NioEventLoopGroup();  //接受请求

		try {
			Bootstrap bootstrap = new Bootstrap();
			bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).            //这里告诉Channel如何接收新的连接
			handler(new ProtoBufClientChannelInitializer());                              //指定初始化channel对象
			ChannelFuture channelFuture = bootstrap.connect(address, port).sync();   //连接到服务器
			channelFuture.channel().closeFuture().sync();
		} catch (Exception e) {
			eventLoopGroup.shutdownGracefully();
			e.printStackTrace();
		}finally {
			eventLoopGroup.shutdownGracefully();
		}
	}
	
	public static void main(String[] args) {
		WalkerProtoBufClient beatClient = new WalkerProtoBufClient("localhost", 8891);
		beatClient.start();
	}

}
